package cn.joyfulmedia.controller.system.resources;


import cn.joyfulmedia.common.util.Const;
import cn.joyfulmedia.entity.ExamMisResources;
import cn.joyfulmedia.entity.ExamMisRole;
import cn.joyfulmedia.mis.service.interfaces.ResourcesService;
import cn.joyfulmedia.mis.service.interfaces.RoleService;
import cn.joyfulmedia.shiro.ShiroSessionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/resources")
public class ResourcesController {
    @Resource
    private ResourcesService resourcesService;
    @Resource
    private RoleService roleService;

    @ResponseBody
    @RequestMapping("/currentResources.json")
    public List<ExamMisResources> currentResources() {
        Long accountId = ShiroSessionUtils.getLoginAccount().getId().longValue();
        try {
//			return resourcesService.findByRoleId(accountId);

            List<ExamMisRole> roleList = roleService.findByAccountId(accountId);

            List<ExamMisResources> list = new ArrayList<>();

            for (ExamMisRole role : roleList) {
                List<ExamMisResources> resources = resourcesService.findByRoleId(role.getId().longValue());
                list.addAll(resources);
            }

            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/list")
    public String list(ExamMisResources resource, Model model) {
        List<ExamMisResources> list = resourcesService.list(resource);
        model.addAttribute("list", list);
        return "/resources/list";
    }

    @RequiresPermissions("sys:perm:add")
    @RequestMapping("/add")
    public String add(Long id, Model model) {
        ExamMisResources r = new ExamMisResources();
        r.setType(0);
        List<ExamMisResources> list = resourcesService.list(r);
        model.addAttribute("list", list);
        return "/resources/add";
    }

    @ResponseBody
    @RequestMapping("/getById/{id}")
    public Object getById(@PathVariable Long id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Const.STATUS, -1);
        try {
            ExamMisResources r = resourcesService.selectResourcesById(id);
            map.put(Const.STATUS, 1);
            map.put("data", r);
        } catch (Exception e) {
            e.printStackTrace();
            map.put(Const.MSG, Const.DEFAULT_ERROR);
        }
        return map;
    }

    @RequestMapping("/save")
    @ResponseBody
    public Object save(ExamMisResources resources) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Const.STATUS, -1);
        try {
            Integer i = 0;
            if (resources.getId() != null && resources.getId() != 0) {
                i = resourcesService.updateResourcesById(resources);
            } else {
                i = resourcesService.saveResources(resources);
            }
            if (i > 0) {
                map.put(Const.STATUS, 0);
                map.put("data", "保存成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put(Const.MSG, Const.DEFAULT_ERROR);
        }
        return map;
    }
}
